#!/usr/bin/env bash

# 文件拷贝通用函数
function copy_file(){
    src=$1
    dest=$2
    if test -z "${src}" || test -z "${dest}"; then
        echo "src and dest parameter should exist"
        return 1
    fi

    cp ${src} ${dest}
}

function install_software() {
    software_name=$1
    version=$2
    force_flag=$3
    if test -z $software_name; then
        echo name of software needed should exist
        return 1
    fi

    case "$ID" in
        ubuntu*)
            if ! which ${software_name} >/dev/null 2>&1; then
                echo installing software ${software_name}
                if test -n "${version}"; then
                    apt-get install -y ${software_name}=${version}
                else
                    apt-get install -y ${software_name}
                fi
                echo ${software_name} ${version:+version:}${version}  installed successfully
            else
                if test -z ${force_flag}; then
                    echo ${software_name} ${version:+version:}${version} has already been installed
                    return 0
                fi

                #强制重新安装
                echo installing software ${software_name} forcely
                if test -n "${version}"; then
                    apt-get install -y ${software_name}=${version}
                else
                    apt-get install -y ${software_name}
                fi
                echo ${software_name} ${version:+version:}${version}  installed successfully

            fi

            ;;
        rhel*|centos*)
            if ! which ${software_name} >/dev/null 2>&1; then
                echo installing software ${software_name}
                if test -n "${version}"; then
                    apt-get install -y ${software_name}=${version}
                else
                    apt-get install -y ${software_name}
                fi
                echo ${software_name} ${version:+version:}${version}  installed successfully
            else
                if test -z ${force_flag}; then
                    echo ${software_name} ${version:+version:}${version} has already been installed
                    return 0
                fi

                #强制重新安装
                echo installing software ${software_name} forcely
                if test -n "${version}"; then
                    yum install -y ${software_name}-${version}
                else
                    yum install -y ${software_name}
                fi
                echo ${software_name} ${version:+version:}${version}  installed successfully

            fi
            ;;
    esac

}

function replace_lastmatch(){
    match_str=$1
    replace_str=$2
    match_file=$3
    match_line=$(grep -n "${match_str}" "${match_file}" | tail -n 1 | cut -f 1 -d:)
    if test -n "${match_line}"; then
        echo need to replace "${match_str}" to "${replace_str}" in file ${match_file}
        sed -i "${match_line} s/${match_str}/${replace_str}/" ${match_file}
    else
        echo do not find "${match_str}" in file ${match_file}
    fi

}

function insert_after_lastmatch(){
    match_str=$1
    replace_str=$2
    match_file=$3
    match_line=$(grep -n "${match_str}" "${match_file}" | tail -n 1 | cut -f 1 -d:)
    if test -n "${match_line}"; then
        echo insert new line: ${replace_str} after lineNo.:${match_line} in file ${match_file}
        sed -i "${match_line} a ${replace_str}" ${match_file}
    else
        echo do not find "${match_str}" in file ${match_file}
    fi

}

function function_decorate(){
    function_name=$*
    echo begin to exectue funtcion ${function_name}
    ${function_name}
    echo end to exectue funtcion ${function_name}
}

############end of common utils function#################